package cn.hx.mallorder.controller;

import cn.hx.mallorder.dao.OmsOrderDao;
import cn.hx.mallorder.entity.OmsOrder;
import cn.hx.mallorder.entity.OmsOrderItem;
import cn.hx.mallorder.service.OmsOrderItemService;
import cn.hx.mallorder.service.OmsOrderService;
import cn.hx.mallorder.util.JwtUtils;
import cn.hx.malluser.vo.UmsMemberVo;
import io.jsonwebtoken.Claims;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

@Controller
public class ReceivedController {
    @Resource
    private OmsOrderService orderService;
    @Resource
    private ProductClient productClient;
    @Resource
    private OmsOrderItemService omsOrderItemService;
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @Resource
    private OmsOrderDao orderDao;

    @GetMapping("received")
    public String queryAll(Model map, HttpSession session, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Long id = 0L;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)) {
                        Claims claims = JwtUtils.getClaims(value);
                        id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
                    }
                }
            }
        }
        UmsMemberVo umsMemberVo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        String username = umsMemberVo.getUsername();
        Long id2 = orderService.findId(2L);
        int id1 = omsOrderItemService.findItemById(id2);
        List<Object> sList = productClient.getList(id1);
        session.setAttribute("sList", sList);
        List<OmsOrder> test1 = orderDao.queryByUserName(username);
        List<OmsOrder> test = test1.stream().filter(o -> {
            return o.getStatus() == 2;
        }).collect(Collectors.toList());
        map.addAttribute("list", test);
        return "received";
    }

    @GetMapping("/ReceivedAdd")
    public String ReceivedAdd(Model map, Integer t, HttpSession session, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Long id = 0L;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)) {
                        Claims claims = JwtUtils.getClaims(value);
                        id = Long.valueOf(claims.get("data", Map.class).get("userName").toString());
                    }
                }
            }
        }
        UmsMemberVo umsMemberVo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        String username = umsMemberVo.getUsername();
        Long id2 = orderService.findId(2L);
        int id1 = omsOrderItemService.findItemById(id2);
        List<Object> sList = productClient.getList(id1);
        session.setAttribute("sList", sList);
        List<OmsOrder> test1 = orderDao.queryByUserName(username);
        List<OmsOrder> test = new ArrayList<>();
        for (OmsOrder order : test1) {
            if (order.getStatus() == 2) {
                test.add(order);
            }
        }
        Calendar calendar = Calendar.getInstance();
        Date time = calendar.getTime();
        List<OmsOrder> test2 = new ArrayList<>();
        if (t == 0) {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 3);
            test2 = test1.stream().filter(o -> {
                return time.getTime() <= o.getCreateTime().getTime();
            }).collect(Collectors.toList());
            map.addAttribute("t0", 0);
        }
        if (t == 1) {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 12);
            test2 = test1.stream().filter(o -> {
                return time.getTime() <= o.getCreateTime().getTime();
            }).collect(Collectors.toList());
            map.addAttribute("t1", 1);
        }
        if (t == 2) {
            test2 = test1.stream().filter(o -> {
                return o.getStatus() == 2;
            }).collect(Collectors.toList());
            map.addAttribute("t2", 2);
        }
        map.addAttribute("list", test2);
        return "received";
    }

    //确认收货
    @GetMapping("/confirmReceiptAll")
    public String ReceiptAll(Model map ,long id) {
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setId(id);
        omsOrder.setStatus(3);
        orderDao.updateStatus(omsOrder);
        return "successfulReceipt";
    }

}
